import * as TWEEN from 'tween' // 动画过度库
import { removeOrbitControls } from '@/Utils/global'

import { headersStore } from '@/stores/apiHeader'
import { mapStore } from '@/stores/worldMap'
const useHeadStore = headersStore()
const useMapStore = mapStore()
// 移动相机
export const changeCamera = (objList, camera, data, scene) => {
  // if (isLogin.value === false) return
  if (objList.isClick != true && objList.scopeCode != '0000111104') {
    // 如果什么都没选，就返回，并且开始旋转和关闭弹窗
    // frameDivClose()
    // rotation()
    return
  }
  // frameDivClose()
  let layerObj = scene.getObjectByName(objList.name)
  const timeOut = 2000
  if (layerObj) {
    scene.rotation.y = 0
    data.rotationY = false
    // 放缩系数
    let scale = 2.8
    // 中国区下钻白名单
    const isChinaWhitelist = ['1', '3']
    if (
      objList.scopeCode == '0000111104' &&
      isChinaWhitelist.includes(useHeadStore.type_id)
    ) {
      // 找到 class为orbit-controls的dom元素，然后删除该元素
      removeOrbitControls()
      setTimeout(() => {
        useMapStore.setMapType(true)
        // dispose() // 释放所有图层
      }, timeOut - 10)
    }
    new TWEEN.Tween({
      x: data.lonlat.x,
      y: data.lonlat.y,
      z: data.lonlat.z,
    })
      .to(
        {
          x: layerObj.position.x * scale,
          y: layerObj.position.y * scale,
          z: layerObj.position.z * scale,
        },
        timeOut // 动画时间 毫秒
      )
      .onUpdate(function () {
        camera.position.x = this.x
        camera.position.y = this.y
        camera.position.z = this.z
        camera.lookAt(0, 0, 0)
      })
      // .onComplete(() => {
      //   retrievalLayer(objList.name)
      // })
      .easing(TWEEN.Easing.Sinusoidal.InOut)
      .start()
    data.lonlat = camera.position
    // 弹窗面板赋值
    // data.countryName = objList.name
    return true
  } else {
    console.log(
      '图层数据已被全部删除，请重新刷新界面，或者重新调用数据初始化方法: this.infoMap ()'
    )
    return false
  }
}
